/*
 * @(#)LocalDbDataSource.java   1.0  2021年11月16日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;

import com.swift.exception.ServiceException;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明
 * 
 * @author zhengjiajin
 * @version 1.0 2021年11月16日
 */
@Component
public class LocalDbDataSource {

    private Connection conn;

    @PostConstruct
    private void init() throws Exception {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        flushConnection();
    }

    private void flushConnection() {
        try {
            if (conn != null) {
                if (!conn.isClosed()) {
                    return;
                }
            }
            createConnection();
        } catch (Exception e) {

        }
    }

    private void createConnection() {
        String URL = "jdbc:derby:dtsdb;create=true";
        try {
            conn = DriverManager.getConnection(URL);
            conn.setAutoCommit(true);
        } catch (Exception ex) {
        }
    }

    public Connection getConnection() {
        flushConnection();
        return conn;
    }
    
    public Statement getStatement() {
        try {
            Statement stmt = getConnection().createStatement();
            return stmt;
        } catch (SQLException e) {
        }
        return null;
    }
    
    public void closeStatement(Statement stmt) {
        try {
            if(stmt!=null)stmt.close();
        } catch (SQLException e) {
        }
    }
    
    public void execute(String sql) {
        try {
            Statement stmt = getStatement();
            stmt.execute(sql);
            closeStatement(stmt);
        } catch (SQLException e) {
            throw new ServiceException("执行SQL出错",e);
        }
    }
    
    public List<Map<String,Object>> queryForList(String sql){
        try {
            List<Map<String,Object>> list = new LinkedList<>();
            Statement stmt = getStatement();
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            while(rs.next()) {
                Map<String,Object> map = new HashMap<>();
                for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {
                    String columnName = rsmd.getColumnName(i);
                    map.put(columnName.toLowerCase(), rs.getObject(columnName));
                }
                list.add(map);
            }
            rs.close();
            closeStatement(stmt);
            return list;
        } catch (SQLException e) {
            throw new ServiceException("执行SQL出错",e);
        }
    }
    
    public Map<String,Object> queryForMap(String sql){
        List<Map<String,Object>> list = queryForList(sql);
        if(TypeUtil.isNull(list)) return null;
        return list.get(0);
    }
    
}
